#include "Stack.h"

void STInit(ST* pst)
{
    assert(pst);
    pst->a = NULL;
    pst->capacity = pst->Top = 0;
}
void STDestroy(ST* pst)
{
    assert(pst);
    free(pst->a);
    pst->a = NULL;
    pst->capacity = pst->Top = 0;
}
void STCheckCapacity(ST* pst)
{
    assert(pst);
    if (pst->capacity == pst->Top)
    {
        int newcapacity = pst->capacity == 0 ? 4 : 2 * pst->capacity;
        STDataType* tmp = (STDataType*)realloc(pst->a, sizeof(STDataType) * newcapacity);
        if (tmp == NULL)
        {
            perror("realloc fail");
            return;
        }
        pst->a = tmp;
        pst->capacity = newcapacity;
    }


}
void STpush(ST* pst, STDataType x)
{
    assert(pst);
    STCheckCapacity(pst);
    pst->a[pst->Top++] = x;
}
STDataType STPop(ST* pst)
{
    assert(pst && pst->Top > 0);
    STDataType ret = pst->a[pst->Top-1];
    pst->Top--;
    return ret;

}
STDataType STTop(ST* pst)
{
    assert(pst);
    return pst->a[pst->Top - 1];
}
bool STEmpty(ST* pst)
{
    assert(pst);
    return pst->Top == 0;
}